package com.association.main.controller;

import com.association.main.common.R;
import com.association.main.domain.JoinInfo;
import com.association.main.domain.MailRequest;
import com.association.main.domain.Members;
import com.association.main.domain.SendMailRequest;
import com.association.main.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/send-mail")
@Api(value = "发送邮件接口",tags = {"发送邮件接口"})
@Slf4j
public class SendMailController {
    @Value("${title}")
    private String title;
    @Autowired
    private MailService mailService;
    @Autowired
    private JoinInfoService joinInfoService;

    @Autowired
    private MembersService membersService;

    @PostMapping("/event")
    public R<Integer> eventEmail(@RequestBody SendMailRequest request){
        int i = this.SendSimpleMessage(request, title+"活动");
        return R.success(i);
    }
    @PostMapping("/vote")
    public R<Integer> voteEmail(@RequestBody SendMailRequest request){
        log.info(request+"");
        int i = this.SendSimpleMessage(request, title+"新投票");
        return R.success(i);
    }
    @PostMapping("/notification")
    public R<Integer> notificationEmail(@RequestBody SendMailRequest request){
        int i = this.SendSimpleMessage(request, title+"公告");
        return R.success(i);
    }




    public int  SendSimpleMessage(SendMailRequest request,String type) {
        int[] successCount = {0};//发送邮箱个数
        MailRequest mailRequest = new MailRequest();
        mailRequest.setSubject(request.getClubs().getName()+type+"通知：" + request.getName());
        mailRequest.setText(request.getDescription() + "<br><br>\t\t\t\t\t----"+request.getClubs().getName());
        LambdaQueryWrapper<JoinInfo> joinInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        joinInfoLambdaQueryWrapper.eq(JoinInfo::getClubId, request.getClubs().getId());
        List<JoinInfo> list = joinInfoService.list(joinInfoLambdaQueryWrapper);
        if(list.size()>0){
            List<Integer> memberIds = list.stream().map(JoinInfo::getMemberId).collect(Collectors.toList());
            List<Members> members = membersService.listByIds(memberIds);

            if(members.size()>0){
                members.forEach(member -> {
                    mailRequest.setSendTo(member.getEmail());
                    if (mailService.sendHtmlMail(mailRequest)) {
                        successCount[0]++;
                    }
                });
            }
        }

        return successCount[0];
    }
}
